﻿#pragma once
#pragma once
#include<iostream>

using namespace std;


enum colour
{
	RED,
	BLACK
};



template<class T>
struct RBTreeNode
{
	T _date;
	RBTreeNode<T>* _left;
	RBTreeNode<T>* _right;
	RBTreeNode<T>* _parent;

	colour _col;
	RBTreeNode(const T& date)
		:_date(date)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
	{

	}

};



template<class T,class Ref,class Ptr>
struct RBTreeIterator
{
	 typedef RBTreeNode<T> Node;
	typedef RBTreeIterator<T,Ref,Ptr> Self;
	Node* _node;
	Node* _root;

	RBTreeIterator(Node* node,Node* root)
		:_node(node)
		,_root(root)
	{

	}

	Ref operator*()
	{
		return _node->_date;
	}
	Ptr operator->()
	{
		return &_node->_date;
	}


	Self& operator++()
	{
		if (_node->_right)
		{
			Node* minleft = _node->_right;
			while (minleft->_left)
			{
				minleft = minleft->_left;
			}
			_node = minleft;
		}
		else
		{
			Node* cur = _node;
			Node* Parent = cur->_parent;
			while (Parent && cur==Parent->_right)
			{
				cur = Parent;
				Parent = Parent->_parent;
			}
			_node = Parent;
		}
		return *this;


	}

	Self& operator--()
	{
		if (_node == nullptr)
		{
			Node* rightmost = _root;
			while (rightmost && rightmost->_right)
			{
				rightmost = rightmost->_right;
			}
			_node = rightmost;

		}
		else if (_node->_left)
		{
			Node* rightmost = _node->_left;
			while (rightmost->_right)
			{
				rightmost = rightmost->_right;
			}
			_node = rightmost;
		}
		else
		{
			Node* cur = _node;
			Node* Parent = _node->_parent;
			while (Parent && cur == Parent->_left)
			{
				cur = Parent;
				Parent = Parent->_parent;
			}
			_node = Parent;
		}
		return *this;


	}

	

	bool operator!=(const Self& s)
	{
		return _node != s._node;
	}
	bool operator==(const Self& s)
	{
		return _node == s._node;
	}

};






template<class K, class T,class keyOfT>
class RBTree
{
	typedef RBTreeNode<T> Node;

public:
	typedef RBTreeIterator<T,T&,T*> Iterator;
	typedef RBTreeIterator<T, const T&,const T*> constIterator;


	RBTree() = default;


	~RBTree()
	{
		Destory(_root);
	}



	Iterator Begin()
	{
		Node* cur = _root;
		while (cur && cur->_left)
		{
			cur = cur->_left;
		}

		return Iterator(cur,_root);

	}


	constIterator End()const
	{
		return constIterator(nullptr,_root);
	}

	constIterator Begin()const
	{
		Node* cur = _root;
		while (cur && cur->_left)
		{
			cur = cur->_left;
		}

		return constIterator(cur, _root);
	}


	Iterator End()
	{
		return Iterator(nullptr, _root);
	}


	pair<Iterator,bool> Insert(const T& date)
	{
		//当插入节点时的树为空时就将新插入的节点置为树的根节点，且颜色变为黑
		if (_root == nullptr)
		{
			_root = new Node(date);
			_root->_col = BLACK;
			return {Iterator(_root,_root),true};
		}

		keyOfT kot;

		//寻找合适的插入位置
		Node* cur = _root;
		Node* Parent = nullptr;
		while (cur)
		{
			if (kot(cur->_date)< kot(date))
			{
				Parent = cur;
				cur = cur->_right;
			}
			else if (kot(cur->_date) > kot(date))
			{
				Parent = cur;
				cur = cur->_left;
			}
			else
			{
				return { Iterator(cur,_root),false };
			}
		}
		//创建新节点
		cur = new Node(date);
		if (kot(Parent->_date) < kot(date))
		{
			Parent->_right = cur;
		}
		else
		{
			Parent->_left = cur;
		}
		cur->_parent = Parent;
		cur->_col = RED;
		Node* ret = cur;
		while (Parent && Parent->_col == RED)
		{
			Node* grandfather = Parent->_parent;

			//父节点为祖父节点的左节点时
			//    g
			//  p  u
			//c
			if (grandfather->_left == Parent)
			{

				Node* uncle = grandfather->_right;
				//叔叔存在且为红
				if (uncle && uncle->_col == RED)
				{
					Parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;
					cur = grandfather;
					Parent = cur->_parent;
				}
				//叔叔不存在或者为黑
				else
				{
					//当c为p的左节点时
					//    g
					//  p  u
					//c
					if (Parent->_left == cur)
					{
						RotateR(grandfather);
						grandfather->_col = RED;
						Parent->_col = BLACK;
					}
					//当c为p的右节点时
					//     g
					//   p   u
					//    c
					else
					{
						RotateL(Parent);
						RotateR(grandfather);
						grandfather->_col = RED;
						cur->_col = BLACK;

					}
					break;


				}

			}
			//父节点为祖父节点的右节点时
			//    g
			//  u   p
			//       c
			else
			{

				Node* uncle = grandfather->_left;
				//叔叔存在且为红
				if (uncle && uncle->_col == RED)
				{
					Parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;
					cur = grandfather;
					Parent = cur->_parent;
				}
				//叔叔不存在或者为黑
				else
				{
					//当c为p的右节点时
					//    g
					//  u   p
					//       c
					if (Parent->_right == cur)
					{
						RotateL(grandfather);
						grandfather->_col = RED;
						Parent->_col = BLACK;
					}
					//当c为p的左节点时
					//    g
					//  u   p
					//     c
					else
					{
						RotateR(Parent);
						RotateL(grandfather);
						grandfather->_col = RED;
						cur->_col = BLACK;

					}
					break;


				}

			}


		}

		//无论以上是否进行调整都将根结点的颜色置为黑
		_root->_col = BLACK;
		return  { Iterator(ret,_root),true };

	}



		Iterator Find(const K& val)
		{
		if (_root == nullptr)
		{
			return nullptr;
		}
		Node* cur = _root;
		while (cur)
		{
			if (cur->_val.first < val)
			{
				cur = cur->_left;
			}
			else if (cur->_val.first > val)
			{
				cur = cur->_right;
			}
			else
			{
				return Iterator(cur,_root);
			}
		}
		return End();
	}


	

	



private:
	Node* _root = nullptr;


	void RotateR(Node* Parent)
	{
		Node* subL = Parent->_left;
		Node* subLR = subL->_right;
		if (subLR != nullptr)
		{
			subLR->_parent = Parent;
		}
		Node* tmpNode = Parent->_parent;
		Parent->_left = subLR;
		Parent->_parent = subL;
		subL->_right = Parent;
		if (tmpNode != nullptr)
		{
			if (tmpNode->_left == Parent)
			{
				tmpNode->_left = subL;
			}
			else
			{
				tmpNode->_right = subL;
			}
			subL->_parent = tmpNode;
		}
		else
		{
			_root = subL;
			subL->_parent = nullptr;

		}

	}



	void RotateL(Node* Parent)
	{
		Node* subR = Parent->_right;
		Node* subRL = subR->_left;
		if (subRL != nullptr)
		{
			subRL->_parent = Parent;
		}
		Node* tmpNode = Parent->_parent;
		Parent->_right = subRL;
		Parent->_parent = subR;
		subR->_left = Parent;
		if (tmpNode != nullptr)
		{
			if (tmpNode->_left == Parent)
			{
				tmpNode->_left = subR;
			}
			else
			{
				tmpNode->_right = subR;
			}
			subR->_parent = tmpNode;
		}

		else
		{
			_root = subR;
			subR->_parent = nullptr;

		}

	}



	void Destory(Node* root)
	{
		if (root == nullptr)return;
		Destory(root->_left);
		Destory(root->_right);
		delete root;
	}


	

};